package com.czt.mail.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author czt
 * @version 1.0
 * {@desc}
 * @date 2024/8/2 15:43
 */
@Component
public class ListRedis {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    public void testList() {
        ListOperations<Object, Object> listOperations = redisTemplate.opsForList();
        String key  = "list" + UUID.randomUUID().toString().substring(0,6);
        for (int i = 0; i < 4; i++) {
            listOperations.leftPush(key, String.valueOf(1));
        }

        //只有key存在，才能设置过期时间
        redisTemplate.expire(key, 1, TimeUnit.DAYS);

        List<Object> list = listOperations.range(key, 0, -1);
        list.forEach(System.out::println);
    }
}
